package com.fangcun.service.admin.floor.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fangcun.bean.admin.floor.FloorReserveAdminResponse;
import com.fangcun.bean.admin.floor.FloorTeahouseSearchRequest;
import com.fangcun.bean.admin.floor.FloorTeahouseSearchResponse;
import com.fangcun.bean.api.floor.request.FloorRoomSearchRequest;
import com.fangcun.bean.api.floor.response.FloorResponse;
import com.fangcun.bean.sys.PageBean;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProAffairException;
import com.fangcun.common.exception.ProRunTimeException;
import com.fangcun.common.util.qiniu.QiniuUtils;
import com.fangcun.entity.Floor;
import com.fangcun.entity.FloorReserve;
import com.fangcun.entity.FloorRoom;
import com.fangcun.entity.FloorRoomDetail;
import com.fangcun.entity.Teahouse;
import com.fangcun.mapper.FloorMapper;
import com.fangcun.mapper.FloorReserveMapper;
import com.fangcun.mapper.FloorRoomDetailMapper;
import com.fangcun.mapper.FloorRoomMapper;
import com.fangcun.mapper.TeahouseMapper;
import com.fangcun.service.admin.floor.FloorAdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * @作者： 陳曉鬆 @时间： 2021年1月7日 - 上午10:29:18 @description：->
 */
@Service("floorAdminService")
public class FloorAdminServiceImpl implements FloorAdminService {

	@Autowired
	private QiniuUtils qiniuUtils;

	@Autowired
	private TeahouseMapper teahouseMapper;
	@Autowired
	private FloorMapper floorMapper;
	@Autowired
	private FloorRoomMapper floorRoomMapper;
	@Autowired
	private FloorRoomDetailMapper floorRoomDetailMapper;
	@Autowired
	private FloorReserveMapper floorReserveMapper;

	@Override
	public PageInfo<FloorTeahouseSearchResponse> teahouseList(PageBean<FloorTeahouseSearchRequest> request) throws ProAffairException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<FloorTeahouseSearchResponse> list = teahouseMapper.listForAdmin(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	@Transactional
	public void add(Teahouse request) throws ProAffairException {
		request.setBusinessLicense(qiniuUtils.uploadImageUrl(request.getBusinessLicense()));
		if (teahouseMapper.insertSelective(request) <= 0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	@Transactional
	public void update(Teahouse request) throws ProAffairException {
		request.setBusinessLicense(qiniuUtils.uploadImageUrl(request.getBusinessLicense()));
		if (teahouseMapper.updateByPrimaryKeySelective(request) <= 0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public List<FloorResponse> floorList(Long teahouseId) throws ProAffairException {
		List<FloorResponse> floorList = floorMapper.selectByTeahouseId(teahouseId);
		if (floorList == null || floorList.size() == 0) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		for (FloorResponse floor : floorList) {
			FloorRoomSearchRequest request = new FloorRoomSearchRequest();
			request.setTeahouseId(floor.getId());
			floor.setFloorRooms(floorRoomMapper.selectByTeahouseIdAndTime(request));
		}
		return floorList;
	}

	@Override
	@Transactional
	public void floorAdd(Floor request) throws ProAffairException {
		if (floorMapper.insertSelective(request) <= 0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	@Transactional
	public void floorUpdate(Floor request) throws ProAffairException {
		if (floorMapper.updateByPrimaryKeySelective(request) <= 0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	@Transactional
	public void roomAdd(FloorRoom request) throws ProAffairException {
		if (floorRoomMapper.insertSelective(request) <= 0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	@Transactional
	public void roomUpdate(FloorRoom request) throws ProAffairException {
		if (floorRoomMapper.updateByPrimaryKeySelective(request) <= 0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public FloorRoomDetail roomDetail(Long roomId) throws ProAffairException {
		FloorRoomDetail detail = floorRoomDetailMapper.selectByPrimaryKey(roomId);
		if (detail == null) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return detail;
	}

	@Override
	public void roomDetailUpdate(FloorRoomDetail request) throws ProAffairException {
		if (floorRoomDetailMapper.updateByPrimaryKeySelective(request) <= 0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public PageInfo<FloorReserveAdminResponse> reserveList(PageBean<FloorReserve> request) throws ProAffairException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<FloorReserveAdminResponse> list = floorReserveMapper.reserveList(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	@Transactional
	public void updateReserve(FloorReserve request) throws ProAffairException {
		if (floorReserveMapper.updateByPrimaryKeySelective(request) <= 0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

}
